package com.study.qqclient.service;

import com.study.qqcommon.Message;
import com.study.qqcommon.MessageType;

import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 提供消息相关的服务
 */
public class MessageClientService {

    /**
     * 发送消息给所有人
     *
     * @param content    消息内容
     * @param senderId   发送者id
     * @param receiverId 接收者id
     */
    public static void sendMessageToOne(String content, String senderId, String receiverId) {
        Message message = new Message();
        message.setSender(senderId);
        message.setGetter(receiverId);
        message.setContent(content);
        message.setMesType(Message.MESSAGE_COMM_MES);
        message.setSendTime(new Date().toString());
        try {
            ObjectOutputStream oos = ManageClientContentServiceThread.getClientConnectServerThread(senderId).getOos();
            oos.writeObject(message);
            System.out.println(senderId + " send message to " + receiverId + ": " + content);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送消息给所有人
     *
     * @param content    消息内容
     * @param senderId   发送者id
     */

    public static void sendMessageToAll(String content, String senderId) {
        Message message = new Message();
        message.setSender(senderId);
        message.setContent(content);
        message.setMesType(MessageType.MESSAGE_TO_ALL_MES);
        message.setSendTime(new Date().toString());

        try {
            ObjectOutputStream oos = ManageClientContentServiceThread.getClientConnectServerThread(senderId).getOos();
            oos.writeObject(message);
            System.out.println(senderId + " send message to all: " + content);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
